package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.R.R;
import net.wanho.dto.ProductDTO;
import net.wanho.dto.ProductQueryDTO;
import net.wanho.po.Product;
import net.wanho.service.ProductService;
import net.wanho.vo.PageInfo;
import net.wanho.vo.ProductExportVo;
import net.wanho.vo.ProductVo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/product")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;

    // 分页查询商品列表
    @GetMapping("/product-page-list")
    public R productPageList(@RequestParam(defaultValue = "1") int pageNum,
                             @RequestParam(defaultValue = "5") int pageSize,
                             ProductDTO productDTO) {
        PageInfo<ProductVo> pageInfo = productService.pageList(pageNum, pageSize, productDTO);
        return R.ok(pageInfo);
    }

    // 添加商品
    @PostMapping("/product-add")
    public R productAdd(@RequestBody Product product) {
        productService.addProduct(product);
        return R.ok("商品添加成功");
    }

    // 根据id查询商品信息
    @GetMapping("{id}")
    public R productInfo(@PathVariable Integer id) {
        Product product = productService.getById(id);
        return R.ok(product);
    }

    // 编辑商品信息
    @PutMapping("/product-update")
    public R productUpdate(@RequestBody Product product) {
        productService.updateById(product);
        return R.ok("商品修改成功");
    }

    // 删除商品
    @DeleteMapping("/product-delete/{id}")
    public R productDelete(@PathVariable Integer id) {
        productService.deleteProduct(id);
        return R.ok("商品删除成功");
    }

    // 批量删除商品
    @DeleteMapping("/product-list-delete")
    public R productListDelete(@RequestBody Integer[] ids) {
        productService.removeBatchByIds(Arrays.asList(ids));
        return R.ok("商品批量删除成功");
    }

    // 导出数据
    @GetMapping("/exportTable")
    public R exportTable(ProductQueryDTO productQueryDTO) {
        List<ProductExportVo> list = productService.exportTable(productQueryDTO);
        return R.ok("导出成功", list);
    }

    // 上架下架
    @PutMapping("/state-change")
    public R stateChange(@RequestBody Product product) {
        productService.updateById(product);
        return R.ok("状态修改成功");
    }
}